package com.kaizeli.website.context;

import java.util.Map;

public class BaseContext {
    public static final String KEY_USER_ID = "userId";
    public static final String KEY_USERNAME = "username";
    public static final String KEY_PERMISSIONS = "permissions";
    public static final String KEY_ROLES = "roles";

    public static ThreadLocal<Map<String, String>> threadLocal = new ThreadLocal<>();

    public static void setCurrentId(String id) {
        set(KEY_USER_ID, id);
    }

    public static String getCurrentId() {
        return get(KEY_USER_ID);
    }

    public static void setPermissions(String permissions) {
        set(KEY_PERMISSIONS, permissions);
    }

    public static String getPermissions() {
        return get(KEY_PERMISSIONS);
    }



    public static void clearContext() {
        threadLocal.remove();
    }

    private static void set(String key, String value) {
        Map<String, String> context = threadLocal.get();
        if (context == null) {
            context = new java.util.HashMap<>();
            threadLocal.set(context);
        }
        context.put(key, value);
    }

    private static String get(String key) {
        Map<String, String> context = threadLocal.get();
        if (context == null) {
            return null;
        }
        return context.get(key);
    }

}
